<template>
  <div class="employees">
    <!-- 工具栏 -->
    <pagetools :showLeft="true">
      <template #left>
        员工管理
      </template>
      <template #right>
        <el-button @click="toImport" size="mini" type="primary">导入</el-button>
        <el-button @click="openAdd" size="mini" type="primary"
          >+ 新增员工</el-button
        >
      </template>
    </pagetools>
    <!-- 表格区域 -->
    <el-card class="macard">
      <el-table :data="list" border>
        <el-table-column
          label="序号"
          type="index"
          :index="indexFn"
          width="60"
          fixed="left"
        ></el-table-column>
        <el-table-column
          label="姓名"
          prop="username"
          sortable
          width="120"
        ></el-table-column>
        <el-table-column label="图片">
          <template slot-scope="{ row }">
            <img
              @click="previewImg(row.staffPhoto)"
              v-imgerror="defaultImg"
              class="myimg"
              :src="row.staffPhoto"
              alt=""
            />
          </template>
        </el-table-column>
        <el-table-column
          label="手机号"
          prop="mobile"
          sortable
          width="120"
        ></el-table-column>
        <el-table-column label="工号" prop="workNumber"></el-table-column>
        <el-table-column width="120" label="聘用形势" sortable>
          <template slot-scope="{ row }">
            {{ row.formOfEmployment | myemploy }}
          </template>
        </el-table-column>
        <el-table-column
          label="部门"
          prop="departmentName"
          sortable
          width="120"
        ></el-table-column>
        <el-table-column label="入职时间" width="120">
          <template slot-scope="{ row }">
            {{ row.timeOfEntry | mymoment }}
          </template>
        </el-table-column>
        <el-table-column label="是否在职" width="120">
          <template>在职</template>
        </el-table-column>
        <el-table-column label="状态" width="120">
          <template>可用</template></el-table-column
        >
        <el-table-column label="操作" width="280" fixed="right">
          <template slot-scope="{ row }">
            <el-button @click="toDetail(row)" type="text">查看</el-button>
            <el-button type="text">转正</el-button>
            <el-button type="text">调岗</el-button>
            <el-button type="text">离职</el-button>
            <el-button @click="openRole(row.id)" type="text">角色</el-button>
            <el-button type="text">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
      <!-- 分页组件 -->
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="page"
        :page-sizes="pagesizes"
        :page-size="pagesize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
      >
      </el-pagination>
    </el-card>
    <!-- 新增面板 -->
    <mypanel :dialogShow.sync="dialogShow" @updateData="getEmpList"></mypanel>
    <!-- 预览图片 -->
    <el-dialog title="预览图片" :visible.sync="show">
      <!-- <img :src="mysrc" /> -->
      <canvas ref="mycanvas"></canvas>
    </el-dialog>
    <!-- 分配角色面板 -->
    <el-dialog title="分配角色" :visible.sync="roleShow">
      <el-form>
        <el-form-item>
          <div>{{ roleList }}</div>
          <el-checkbox-group v-model="roleList">
            <el-checkbox
              v-for="(item, index) in allRole"
              :key="index"
              :label="item.id"
              >{{ item.name }}</el-checkbox
            >
          </el-checkbox-group>
        </el-form-item>
        <el-form-item>
          <div style="text-align: center">
            <el-button type="primary">确定</el-button>
            <el-button @click="roleDialog = false">取消</el-button>
          </div>
        </el-form-item>
      </el-form>
    </el-dialog>
  </div>
</template>

<script>
import QRcode from 'qrcode'
import { getEmpListApi, getBaseInfoApi, setRoleAPi } from '@/api/employees'
import { getAllRoleApi } from '@/api/role'
export default {
  data () {
    return {
      // 表格的数据源
      list: [],
      // 当前页
      page: 1,
      // 页容量的集合
      pagesizes: [5, 10, 15, 20],
      // 页容量
      pagesize: 10,
      // 总条数
      total: 0,
      // 控制新增面板的显示与隐藏
      dialogShow: false,
      defaultImg: require('@/assets/common/head.jpg'),
      // 控制预览面板的显示与隐藏
      show: false,
      // 预览框中的图片路径
      mysrc: '',
      // 控制角色面板的显示与隐藏
      roleShow: false,
      // 选中的角色的数组
      roleList: [],
      // 所有的角色列表
      allRole: [],
      currentId: []
    }
  },
  methods: {
    // 获取所有的角色列表
    async getAllRole () {
      const res = await getAllRoleApi()
      // 保存所有的角色列表
      this.allRole = res.data.rows
    },
      async submit () {
      await setRoleAPi({
        id: this.currentId,
        roleIds: this.roleList
      })
      this.$message.success('修改成功')
      this.roleShow = false
      this.getEmpList()
    },
    // 打开角色面板
    async openRole (id) {
      // id 当前点击数据的 id
      //保存当前员工的 id
      this.currentId = id
      this.roleShow = true
      // 得到当前点击员工的角色信息
      const res = await getBaseInfoApi(id)
      // 保存当前点击员工的角色信息
      this.roleList = res.data.roleIds
    },
    previewImg (src) {
      this.show = true
      this.mysrc = src
      this.$nextTick(() => {
        // 将 mysrc 转为二维码
        QRcode.toCanvas(this.$refs.mycanvas, this.mysrc, {
          width: 200,
          height: 200
        })
      })
    },
    // 跳转到详情页面
    toDetail (row) {
      // console.log(row);
      this.$router.push(`/employees/detail?id=${row.id}`)
    },
    // 跳转到 import 中
    toImport () {
      this.$router.push('/employees/import')
    },
    indexFn (index) {
      // 第1页 => 1
      // 第2页 => 11
      return (this.page - 1) * this.pagesize + index + 1
    },
    // 打开新增面板
    openAdd () {
      this.dialogShow = true
    },
    // 得到所有的员工数据
    async getEmpList () {
      const res = await getEmpListApi({
        page: this.page,
        size: this.pagesize
      })
      // 保存
      this.list = res.data.rows
      this.total = res.data.total
    },
    // 当页容量改变时会触发
    handleSizeChange (size) {
      this.pagesize = size
      // 重新请求
      this.getEmpList()
    },
    // 当前页改变时会触发
    handleCurrentChange (page) {
      this.page = page
      // 重新请求
      this.getEmpList()
    }
  },
  created () {
    // 得到员工列表
    this.getEmpList()
    // 获取所有的角色列表
    this.getAllRole()
  },
  components: {
    mypanel: () => import('./components/mypanel.vue')
  }
}
</script>

<style lang="scss">
.employees {
  margin: 20px;
  .macard {
    margin-top: 20px;
  }
  .el-pagination {
    margin-top: 20px;
    text-align: right;
  }
  .myimg {
    width: 40px;
    height: 40px;
    border-radius: 50%;
  }
}
</style>
